package com.swt.dao.Tdao.impl;

import com.swt.dao.Tdao.CoursePKDao;
import com.swt.domain.Course;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository(value = "CoursePKDao")
public class CoursePKDaoImpl implements CoursePKDao {
    @Resource
    private HibernateTemplate hibernateTemplate;
    //    排课添加
    @Override
    public boolean coursePKDaoAdd(Course course,int clubClubId) {

            course.setClubClubId(clubClubId);
        try {
            hibernateTemplate.save(course);

        }catch (Exception e){
            return false;
        }

        return true;
    }
    //查询排课表
    @Override
    public List<Course> coursePKDaoQuery(int courseClubid,int clubClubId) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();


        String sql="from Course where courseClubid = ? and clubClubId = ? ";
        Query query1 = session.createQuery(sql);
        List<Course> courseList =  query1.setParameter(0,courseClubid).setParameter(1,clubClubId).list();
        return courseList;
    }
    //根据对象条件查询
    @Override
    public List<Course> coursePKDaoUPdateQuery(Course course,int clubClubId) {
        course.setClubClubId(clubClubId);
//        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
//        Session session = sessionFactory.getCurrentSession();
//        String sql="from  Course where courseName = ? and courseTeacher = ? and courseTime = ? and courseDate = ?";
//        Query query = session.createQuery(sql);
//        List<Member> memberList = query.setParameter(0,1).setParameter(1,2).list();

        List< Course> courseList = hibernateTemplate.findByExample(course);
        return courseList;
    }

    //    修改排课
    @Override
    public boolean coursePKDaoUPdate(Course course) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        try {
            session.update(course);
        }catch (Exception e){
            return false;
        }
        return true;

    }
    //删除排课
    @Override
    public boolean coursePKDaoDelete(int id) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Course course = session.get(Course.class,id);
        try {
            session.delete(course);
        }catch (Exception e){
            return false;
        }

        return true;

    }

    @Override
    public List<Course> coursePKQueryTime(String courseDate, String courseTime,int clubClubId) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        String sql = "from  Course where courseDate = ? and courseTime = ? and clubClubId = ?";
        Query query = session.createQuery(sql);
        List<Course> courseList = query.setParameter(0,courseDate).setParameter(1,courseTime).setParameter(2,clubClubId).list();
        return courseList;
    }

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }
}
